<?php 
namespace app\admin\controller;

use think\Db;
use think\Exception;
use app\common\model\user\UserSignParam;
use app\common\controller\Log;
use app\common\model\admin\AdminUser;

class Sign extends Base
{
	/**
	 * 发布活动报名
	 * @throws Exception
	 * @return unknown
	 */
	public function setSignParam()
	{
		$data = $this->request->post('');
		Db::startTrans();
		try {
			if(!$data['name'])throw new Exception('请输入报名名称');
			if ($data['age_end'] < $data['age_start'])throw new Exception('年龄区间不正确');
			if ($data['height_start'] > $data['height_end'])throw new Exception('男生身高区间不正确');
			if ($data['weight_start'] > $data['weight_end'])throw new Exception('男生体重区间不正确');
			if ($data['height1_start'] > $data['height1_end'])throw new Exception('女生身高区间不正确');
			if ($data['weight1_start'] > $data['weight1_end'])throw new Exception('女生体重区间不正确');
			if (!empty($data['start_time']) && !empty($data['end_time'])) {
				$data['start_time'] = strtotime($data['start_time']);
				$data['end_time'] = strtotime($data['end_time']);
				if($data['start_time'] > $data['end_time']) throw new Exception('报名时间选择错误');
			} else {
				throw new Exception('活动时间选择错误');
			}
			$data['create_time'] = time();
			$data['update_time'] = time();
			$data['send_admin'] = $this->admin_id;
			UserSignParam::create($data);
			//添加日志
			Log::addLog(1, '添加报名【'.$data['name'].'】');
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
			return jsonError($e->getMessage());
		}
		return jsonSuccess('提交成功');
	}
	
	/**
	 * 报名列表
	 * @return unknown
	 */
	public function getSignList()
	{
		$data = $this->request->post('');
		//初始化查询时间
		$where = []; 
		//时间查询
		if(!empty($data['times']) && count($data['times']) == 2) {
			$start_time = strtotime($data['times'][0]);
			$end_time = strtotime($data['times'][1]) + 24*60*60;
			$where['create_time'] = ['between',[$start_time,$end_time]]; 
		}
		//发布人查询
		if(!empty($data['send_admin'])) {
			$admin_id1 = AdminUser::where(['admin_real' => ['like','%'.$data['send_admin'].'%']])->column('id');
			$admin_id2 = AdminUser::where(['admin_name' => ['like','%'.$data['send_admin'].'%']])->column('id');
			$where['send_admin'] = ['in',array_merge($admin_id1,$admin_id2)];
		}
		$list = UserSignParam::where($where)->order('state desc,id desc')->paginate()->toArray();
		return jsonSuccess($list);
	}
	
	/**
	 * 获取报名详情
	 * @return unknown
	 */
	public function signDetail()
	{
		$id = $this->request->post('id');
		$info = UserSignParam::get($id)->toArray();
		unset($info['send_admin']);
		return jsonSuccess($info);
	}
	
	/**
	 * 修改报名信息
	 * @throws Exception
	 * @return unknown
	 */
	public function updateSign()
	{
		$data = $this->request->post('');
		Db::startTrans();
		try {
			if(!$data['name'])throw new Exception('请输入报名名称');
			if ($data['age_end'] < $data['age_start'])throw new Exception('年龄区间不正确');
			if ($data['height_start'] > $data['height_end'])throw new Exception('男生身高区间不正确');
			if ($data['weight_start'] > $data['weight_end'])throw new Exception('男生体重区间不正确');
			if ($data['height1_start'] > $data['height1_end'])throw new Exception('女生身高区间不正确');
			if ($data['weight1_start'] > $data['weight1_end'])throw new Exception('女生体重区间不正确');
			if (!empty($data['start_time']) && !empty($data['end_time'])) {
				$data['start_time'] = strtotime($data['start_time']);
				$data['end_time'] = strtotime($data['end_time']);
				if($data['start_time'] > $data['end_time']) throw new Exception('报名时间选择错误');
			} else {
				throw new Exception('活动时间选择错误');
			}
			unset($data['create_time']);
			$data['update_time'] = time();
			$data['send_admin'] = $this->admin_id;
			$user_sign = UserSignParam::get($data['id']);
			$user_sign->save($data);
			//添加日志
			Log::addLog(2, '修改报名【'.$data['name'].'】');
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
			return jsonError($e->getMessage());
		}
		return jsonSuccess('提交成功');
	}
	
	public function stopSign()
	{
		$id = $this->request->param('id');
		Db::startTrans();
		try {
			$sign = UserSignParam::get($id);
			$sign->save(['state' => 0]);
			//添加日志
			Log::addLog(2, "提前停止【{$sign->name}】报名");
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
			return jsonError($e->getMessage());
		}
		return jsonSuccess('停止成功');
	}
}